package com.example.a14110100420.yuejian.Fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.a14110100420.yuejian.R;
import com.example.a14110100420.yuejian.WActivity.personal.AboutActivity;
import com.example.a14110100420.yuejian.WActivity.personal.DingdanDetailActivity;
import com.example.a14110100420.yuejian.WActivity.personal.FatieDetailActivity;
import com.example.a14110100420.yuejian.WActivity.personal.GuanzhuDetailActivity;
import com.example.a14110100420.yuejian.WActivity.personal.ShezhiActivity;
import com.example.a14110100420.yuejian.WActivity.personal.XiaoxiDetailActivity;
import com.example.a14110100420.yuejian.WSerivice.PersonalService;
import com.example.a14110100420.yuejian.Wbase.BaseActivity;
import com.example.a14110100420.yuejian.Wbase.BaseFragment;
import com.example.a14110100420.yuejian.Wbase.Conast;
import com.example.a14110100420.yuejian.Wbean.personal.Personal;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class MeFragment extends BaseFragment {
    private View view;
    private Intent intent;

    final String userid =getUserid();


    private ImageView Touxiang;

    private String name;
    private TextView UserName;

    private String fnum;
    private TextView FatieNum;
    private String Gnum;
    private TextView GuanzhuNum;
    private  String Dnum;
    private TextView DingdanNum;
    private String Xnum;
    private TextView XiaoxiNum;

    private LinearLayout guanzhu;
    private LinearLayout xiaoxi;
    private LinearLayout fatie;
    private LinearLayout dingdan;
    private LinearLayout shezhi;
    private LinearLayout about;


    public MeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_me, container, false);
        init_view();
        getUserInfo();
        OnClick();

        return view;
    }
    private void init_view(){
//        DingdanNum.findViewById(R.id.Dingdan_Num);
        FatieNum = view.findViewById(R.id.Fatie_Num);
        XiaoxiNum = view.findViewById(R.id.Xiaoxi_Num);
        GuanzhuNum = view.findViewById(R.id.Guanzhu_Num);
        DingdanNum = view.findViewById(R.id.Dingdan_Num);

        UserName = view.findViewById(R.id.my_name);
        Touxiang = view.findViewById(R.id.touxiang);

        fatie = view.findViewById(R.id.Fatie_Detail);
        guanzhu = view.findViewById(R.id.Guanzhu_Detail);
        xiaoxi = view.findViewById(R.id.Xiaoxi_Detail);
        dingdan = view.findViewById(R.id.Dingdan_Detail);

        shezhi = view.findViewById(R.id.xiugai);
        about = view.findViewById(R.id.about);


    }
    private void getUserInfo(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Conast.base_Url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        PersonalService service = retrofit.create(PersonalService.class);
        Call<Personal> call = service.getUser(userid);
        call.enqueue(new Callback<Personal>() {
            @Override
            public void onResponse(Call<Personal> call, Response<Personal> response) {
                Personal data = response.body();

//                if (null == data.getPhoto())
//                    return;
                String userTouxiang= data.getPhoto();
                BaseActivity.setUsertouxiang(userTouxiang);
                if (data!=null){
//
                    Picasso.with(getContext())
                            .load("http://172.24.10.175/workout/Uploads/"+data.getPhoto())
                            .into(Touxiang);  //显示头像
                    name = data.getUser_name();
                    UserName.setText(name); //显示姓名

                    Gnum = data.getFocusnum();
                    GuanzhuNum.setText(Gnum);//显示关注数

                    fnum = data.getShaitunum();
                    FatieNum.setText(fnum);//显示发帖数量

                    Xnum = data.getMessagenum();
                    XiaoxiNum.setText(Xnum); //显示消息数量

                    Dnum = data.getMessagenum();
                    DingdanNum.setText(Dnum);//显示订单数量


                }
            }

            @Override
            public void onFailure(Call<Personal> call, Throwable t) {

            }
        });



    }


    private void OnClick(){
       fatie.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {

               Intent intent = new Intent(getActivity(),FatieDetailActivity.class);
               getActivity().startActivity(intent);
           }
       });
        guanzhu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), GuanzhuDetailActivity.class);
                getActivity().startActivity(intent);
            }
        });
        xiaoxi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                intent.putExtra("uid",userid);
                Intent intent = new Intent(getActivity(), XiaoxiDetailActivity.class);
                getActivity().startActivity(intent);
            }
        });
        dingdan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), DingdanDetailActivity.class);
                getActivity().startActivity(intent);
            }
        });
        shezhi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ShezhiActivity.class);
                getActivity().startActivity(intent);
            }
        });
        about.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AboutActivity.class);
                getActivity().startActivity(intent);
            }
        });


    }



}
